package com.hyj.exception;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import com.hyj.util.Result;


@RestController
@ControllerAdvice
public class ExceptionController {
	
	@ExceptionHandler(BindException.class)
    public Object bindExceptionHandler(BindException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        return new Result(500,"请求参数异常："+objectError.getDefaultMessage());
    }
	
	
	@ExceptionHandler(Exception.class)
	public Object exceptionHandler(Exception e) {
		return new Result(500,"服务器内部异常:"+e.getMessage());
	}

}
